/**
 * Copyright (C) 2021-2022 maminjie <canpool@163.com>
 * SPDX-License-Identifier: LGPL-2.1
 * Powered by Qt-Advanced-Docking-System
**/

#pragma once

#include "qads_global.h"
#include "dockwidget.h"

#include <QWidget>

QT_BEGIN_NAMESPACE
class QAbstractButton;
class QXmlStreamWriter;
QT_END_NAMESPACE

QADS_BEGIN_NAMESPACE

class DockManager;
class DockContainer;
class DockContainerPrivate;
class DockTitleBar;
class DockPannelPrivate;

/**
 * DockPannel manages multiple instances of DockWidgets.
 * It displays a title tab, which is clickable and will switch to
 * the contents associated to the title when clicked.
 */
class QADS_EXPORT DockPannel : public QWidget
{
    Q_OBJECT
public:
    enum DockPannelFlag {
        HideSingleWidgetTitleBar = 0x0001,
        DefaultFlags = 0x0000
    };
    Q_DECLARE_FLAGS(DockPannelFlags, DockPannelFlag)

public:
    DockPannel(DockManager *dockManager, DockContainer *parent);
    ~DockPannel() override;

    DockManager *dockManager() const;
    DockContainer *dockContainer() const;

    DockTitleBar *titleBar() const;
    QRect titleBarGeometry() const;
    QAbstractButton *titleBarButton(TitleBarButtonRole which) const;

    int dockWidgetsCount() const;
    QList<DockWidget *> dockWidgets() const;
    DockWidget *dockWidget(int index) const;

    int openDockWidgetsCount() const;
    QList<DockWidget *> openedDockWidgets() const;

    int indexOfFirstOpenDockWidget() const;

    DockWidget *currentDockWidget() const;
    void setCurrentDockWidget(DockWidget *dockWidget);

    DockWidgetAreas allowedAreas() const;
    void setAllowedAreas(DockWidgetAreas areas);

    DockWidget::DockWidgetFeatures features(BitwiseOperator mode = BitwiseAnd) const;

    DockPannelFlags dockPannelFlags() const;
    void setDockPannelFlags(DockPannelFlags flags);
    void setDockPannelFlag(DockPannelFlag flag, bool on);

    bool isCentralPannel() const;

    int currentIndex() const;
    void setCurrentIndex(int index);

    void closePannel();
    void closeOtherPannels();

    QSize minimumSizeHint() const override;
    QRect contentAreaGeometry() const;

    void setVisible(bool visible) override;

    void saveState(QXmlStreamWriter &stream) const;

signals:
    void currentChanging(int index);
    void currentChanged(int index);
    void tabBarClicked(int index);
    void viewToggled(bool open);

protected:
    void insertDockWidget(int index, DockWidget *dockWidget, bool activate = true);
    void addDockWidget(DockWidget *dockWidget);
    void removeDockWidget(DockWidget *dockWidget);

    void toggleDockWidgetView(DockWidget *dockWidget, bool open);
    void toggleView(bool open);

    DockWidget *nextOpenDockWidget(DockWidget *dockWidget) const;

    int indexOf(DockWidget *dockWidget);

    void hideWithNoVisibleContent();
    void updateTitleBarVisibility();
    void setCurrentDockWidgetInternal(DockWidget *dockWidget);

    void markTitleBarMenuOutdated();

#ifdef Q_OS_WIN
    /**
     * Reimplements QWidget::event to handle QEvent::PlatformSurface
     * This is here to fix issue #294 Tab refresh problem with a QGLWidget
     * that exists since Qt version 5.12.7. So this function is here to
     * work around a Qt issue.
     */
    virtual bool event(QEvent *event) override;
#endif

private:
    void onTabCloseRequested(int index);
    void reorderDockWidget(int fromIndex, int toIndex);

    void onDockWidgetFeaturesChanged();

private:
    DockPannelPrivate *d;
    friend class DockPannelPrivate;
    friend class DockContainer;
    friend class DockContainerPrivate;
    friend class DockTab;
    friend class DockWidgetPrivate;
    friend class DockWidget;
    friend class DockManagerPrivate;
    friend class DockManager;
}; // class DockPannel

QADS_END_NAMESPACE

Q_DECLARE_OPERATORS_FOR_FLAGS(QAds::DockPannel::DockPannelFlags)
